package com.audaque.springboot.foshanupload.security.provider;

import com.audaque.springboot.foshanupload.security.component.MyMd5PasswordEncoder;
import com.audaque.springboot.foshanupload.security.model.MyUserDetail;
import com.audaque.springboot.foshanupload.security.service.impl.MyUserDetailService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * @author zgb
 * @desc 认证服务:每个认证器对应一种登录方式
 * @date 2022-08-05 18:04:27
 */
@Component
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyUsernamePasswordAuthenticationProvider implements AuthenticationProvider {


    @Autowired
    private MyMd5PasswordEncoder myMd5PasswordEncoder;

    @Autowired
    private MyUserDetailService myUserDetailService;




    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getName();
        //根据用户名生成认证令牌：UsernamePasswordAuthenticationToken(myUserDetail, password, authorities);
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = myUserDetailService.getUsernamePasswordAuthenticationTokenByByUsername(username);
        return usernamePasswordAuthenticationToken;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }
}
